package com.chuang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.Uuser;
import com.chuang.bootplus.mapper.UuserMapper;
import com.chuang.bootplus.po.LoginPO;
import com.chuang.bootplus.po.UUserPO;
import com.chuang.bootplus.service.UuserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.vo.UserInfoVO;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class UuserServiceImpl extends ServiceImpl<UuserMapper, Uuser> implements UuserService {

    @Override
    public ApiResponse<UserInfoVO> login(UUserPO po) {
        Uuser uuser=getOne(new LambdaQueryWrapper<Uuser>().eq(Uuser::getUsername,po.getUsername()).eq(Uuser::getPassword,po.getPassword()));
        if(uuser==null){
            throw new BusException("用户名或密码错误");
        }

        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<UserInfoVO> register(UUserPO po) {
        Uuser uuser=getOne(new LambdaQueryWrapper<Uuser>().eq(Uuser::getUsername,po.getUsername()));
        if (uuser!=null) {
            throw new BusException("用户名已注册");
        }else{
            Uuser user= BeanUtil.beanA2beanB(po,Uuser.class);
            save(user);
        }
        return new ApiResponse<>();
    }
}
